<?php

class Earnings

{
                /*function to get the total earnings on the site*/

                function getTotalEarnings(){
                        $query = "SELECT sum( `amount` ) AS `total_earnings` FROM `earnings`";
                        $rs = mysql_query($query);
			$row = mysql_fetch_assoc($rs);
			$nothing = 0;
			if($row['total_earnings'] < 0.01) {
			return $nothing;
			} else {
                        return $row['total_earnings'];
                        }
		}
		
		/*end function */
		
		/*function to get a users total earnings on the site*/
		
		function getUserTotalEarnings($userid){
                        $query = "SELECT sum( `amount` ) AS `total_earnings` FROM `earnings` WHERE `user_id` = '$userid'";
                        $rs = mysql_query($query);
			$row = mysql_fetch_assoc($rs);
			$nothing = 0;
			if($row['total_earnings'] < 0.01) {
			return $nothing;
			} else {
                        return $row['total_earnings'];
                        }
		}
		
		/*end function */
		
		 function getFileTotalEarnings($fileid){
                        $query = "SELECT sum( `amount` ) AS `total_earnings` FROM `earnings` WHERE `file_id` = '$fileid'";
                        $rs = mysql_query($query);
			$row = mysql_fetch_assoc($rs);
			$nothing = 0;
			if($row['total_earnings'] < 0.01) {
			return $nothing;
			} else {
                        return $row['total_earnings'];
                        }
		}
		
		function getReferralEarnings($user_id){
			global $ado;
			$res = $ado->exec("SELECT `referral_percent` from settings");
			$row = mysql_fetch_assoc($res);
			$percent = $row['referral_percent'];
			
			return ($ado->exec("SELECT user_id, (sum(amount)*$percent) as amt from earnings left join users on users.id = earnings.user_id WHERE user_id IN (select id from users where referrer_id = '$user_id') group by user_id"));		
			
		}
		
		 function getUserDetailsWithPendingAmount( $id )
		{	
			global $ado;
			$sql = "SELECT u.*,(select sum(amount) from earnings where user_id = '$id') as total_amount,
					(select sum(payout_amount) from payments where user_id = '$id') as payment_amount FROM  users as u
					WHERE u.id  = '$id' group by u.id";
			$res = $ado->exec($sql);
			if($ado->count($res) >0)
			{			
				$user_details = $ado->fetch($res);
				
				return($user_details);			
			}
			else
			{
				return false;
			}
		}
		
		function getPendingPayment($id){
			global $ado;
			$total_amount = $this->userTotalAmount($id);
			$actual_payment = $this->userActualPayment($id);
			
			$payment_due =  ($total_amount['total_earning'] - $actual_payment['paid_amt']);
			if($payment_due < 0){
				$payment_due = 0;
			}
			return $payment_due;
		}
		
		 function userActualPayment($id)
		{
			global $ado;
			$sql = "select u.id, p.user_id, sum(p.payout_amount) as paid_amt from users as u
			left join payments as p on u.id = p.user_id where user_id = '$id' and p.status<> 'started'  group by u.id";
			$res = $ado->exec($sql);
			return $ret = $ado->fetch($res);
		}
		
		function isPaymentAlreadyRequested($user_id){
			global $ado;
			$current_month_end = date("Y-m-31");
			$current_month_start = date("Y-m-01");	
					
			$res = $ado->exec("SELECT `id` FROM `payment_requests` 
			WHERE `request_date` >= '$current_month_start' AND `request_date` <= '$current_month_end'");
			if(mysql_num_rows($res)){
				return true;
			}else{
				return false;
			}
		}
		
		 function userTotalAmount($id)
		{
			global $ado;
			$sql = "select u.username, u.email, u.id, e.user_id, sum(e.amount) as total_earning from users as u 
			left join earnings as e on u.id = e.user_id where user_id = '$id' group by u.id";
			$res = $ado->exec($sql);
			return $ret = $ado->fetch($res);
			//return $res;
			
		}
}

global $earnings;

$earnings = new Earnings();

?>